package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @GetMapping
    public Result getMenu(String username){
        try {
            List<Menu> list = menuService.findMenuByUserUsername(username);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_SUCCESS);
        }
    }

    //新增菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu, Integer radio) {
        try {
            menuService.add(menu,radio);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPAge(@RequestBody QueryPageBean queryPageBean) {
        return menuService.queryPage(queryPageBean);
    }


    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_MENU_SUCCESS);
        }


    }

    //编辑菜单
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu menu,Integer pid) {
        try {
            menuService.edit(menu,pid);
            return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }

    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            menuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }
    @RequestMapping("/findAllParent")
    public Result findAllParent(){
        try {
            List<Menu> menus = menuService.findAllParent();
            return new Result(true,"查询成功",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    @RequestMapping("/findParentById")
    public Result findParentById(Integer id){
        try {
            Integer pid = menuService.findParentById(id);
            return new Result(true,"查询成功",pid);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
}
